package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.mapper.BookMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.service.ExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2019-09-18 下午6:31
 * Description：<描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    private ExamineMapper examineMapper;
    @Autowired
    private BookMapper bookMapper;

    @Transactional(readOnly = true)
    public PageInfo<Examine> findAll(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Examine> books = examineMapper.selectAll();
        PageInfo<Examine> info = new PageInfo<Examine>(books);
        return info;

    }

    public void addExamine(Examine examine) {
        examineMapper.insertExamine(examine);
    }

    public PageInfo<Examine> findByCondition(Integer page, Integer rows, Examine examine) {
        PageHelper.startPage(page, rows);
        List<Examine> books = examineMapper.selectByCondition(examine);
        PageInfo<Examine> info = new PageInfo<Examine>(books);


        return info;


    }

    public void addInfo(Examine examine) {
        Book book = bookMapper.selectById(examine.getBookId());
        Integer flag = examine.getFlag();
        String title="";
        if(flag==Constant.EXAMINE_YES){
            title =book.getBookName()+"审核通过";
            book.setFlag(Constant.BOOK_ENABLE_STATUS);
            bookMapper.updateBook(book);
        }
        else if(flag==Constant.EXAMINE_NO){
            title =book.getBookName()+"审核未通过";
        }

        examine.setTitle(title);
        examineMapper.updateInfo(examine);
    }
}